package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Fragment fragment1,fragment2,fragment3,fragment4;
    FragmentManager  fm;
    LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        fragment1=new BlankFragment1();
        fragment2=new BlankFragment2();
        fragment3=new BlankFragment3();
        fragment4=new BlankFragment4();

        linearLayout1=findViewById(R.id.LinearLayout1);
        linearLayout2=findViewById(R.id.LinearLayout2);
        linearLayout3=findViewById(R.id.LinearLayout3);
        linearLayout4=findViewById(R.id.LinearLayout4);
        fm=getSupportFragmentManager();

        initial();//4个fragment放一起
        fragmenthide();//藏起来
        fragmentshow(fragment1);//默认的聊天界面

        //点击show出来 switch case

        linearLayout1.setOnClickListener(this::onClick);//监听对屏幕的
        linearLayout2.setOnClickListener(this::onClick);//监听对屏幕的
        linearLayout3.setOnClickListener(this::onClick);//监听对屏幕的
        linearLayout4.setOnClickListener(this::onClick);//监听对屏幕的

    }


    private void fragmentshow(Fragment fragment) {
        FragmentTransaction ft=fm.beginTransaction()
                .show(fragment);
        fragmenthide();
        ft.show(fragment1);
        ft.commit();
    }
    private void fragmenthide() {//藏起来
        FragmentTransaction ft=fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();
    }

    private void initial() {//压入content
        FragmentTransaction ft=fm.beginTransaction()//事物 管理交互动作
                .add(R.id.content,fragment1)
                .add(R.id.content,fragment2)
                .add(R.id.content,fragment3)
                .add(R.id.content,fragment4);
        ft.commit();

    }


    public void onClick(View view) {
        fragmenthide();
        if (view.getId()==R.id.LinearLayout1){
            fragmentshow(fragment1);
        }else if (view.getId()==R.id.LinearLayout2){
            fragmentshow(fragment2);
        }else if (view.getId()==R.id.LinearLayout3){
            fragmentshow(fragment3);
        }else if(view.getId()==R.id.LinearLayout4){
            fragmentshow(fragment4);
        }
//        private void initial() {//压入content
//
//            FragmentTransaction ft=fm.beginTransaction()
//                    .add(R.id.content,BlankFragment1)
//                    .add(R.id.content,BlankFragment2)
//                    .add(R.id.content,BlankFragment3)
//                    .add(R.id.content,BlankFragment4);
//            ft.commit();
//        }
//        private void fragmentshow(Fragment fragment) {
//            FragmentTransaction ft=fm.beginTransaction()
//                    .show(fragment);
//            ft.commit();
//        }
//        switch(view.getId()){
//            case R.id.LinearLayout1:fragmentshow(BlankFragment1);break;
//            case R.id.LinearLayout2:fragmentshow(BlankFragment2);break;
//            case R.id.LinearLayout3:fragmentshow(BlankFragment3);break;
//            case R.id.LinearLayout4:fragmentshow(BlankFragment4);break;
//        }
//        public void onClick(View view) {
//            fragmenthide();
//            if (view.getId()==R.id.LinearLayout1){
//                fragmentshow(BlankFragment1);
//            }else if (view.getId()==R.id.LinearLayout2){
//                fragmentshow(BlankFragment2);
//            }else if (view.getId()==R.id.LinearLayout3){
//                fragmentshow(BlankFragment3);
//            }else if(view.getId()==R.id.LinearLayout4){
//                fragmentshow(BlankFragment4);
//            }
//

//
            ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}